<?php
class PreparationREQ
{
    //Variables qui contiendront la  pr�paration des requ�tes
    private $ReqSelectById;
    private $ReqSelectByName;
    private $ReqInsertEcole;
    private $ReqSelectAllEcole;
    private $ReqInsertEleve;
    private $ReqTruncate;
    private $Reqpage;
    private $validationinsc;
    private $connexion;


    public function __construct($db)
    {
    	//variables renseignées lors de la pr�paration des requ�tes
    	mb_internal_encoding('UTF-8');
	
        $this->ReqSelectAllEcole = $db->prepare("SELECT idecole, nomecole, ville, login, mail, usep FROM ecole ORDER BY nomecole");
		
        $this->ReqSelectById = $db->prepare("SELECT id, name FROM user WHERE id = ?");
       
 	    $this->ReqSelectByName = $db->prepare("SELECT id, name FROM user WHERE name LIKE ? ORDER BY name, id");
        
		$this->ReqInsertEcole = $db->prepare("INSERT INTO ecole(NOMECOLE, VILLE, login, password, mail, usep) VALUES (:name, :city, :login, :password, :mail, :usep)");
		//$this->ReqInsert = $db->prepare("INSERT INTO ecole(nomecole, ville) VALUES (:name, :city)");
		
		$this->Reqpage = $db->prepare("SELECT * FROM pagesite where TITRE=:titre");
		
		$this->validationinsc = $db->prepare("UPDATE ecole SET valid = 1 WHERE NOMECOLE=:nom AND VILLE=:ville");
		
		$this->connexion= $db->prepare("select login, idecole from ecole where login=(:login) and password=(:password)");
		
      	$this->ReqTruncate = $db->prepare("TRUNCATE user");
       
      	$this->ReqInsertEleve = $db->prepare("INSERT INTO eleve  ( `IDCOURSE` , `IDECOLE` , `NOMELEVE` , `ANNEENAISSANCE` , `ADRESSE` , `CERTIFICAT` , `AUTORISATIONP` , `DROITIMAGE` , `EVACUATIONURGENCE` , `NUMLICENSE` ) 
      										  VALUES(:idcourse, :idecole, :nomeleve, :annee, :adresse, :certi, :autoriP, :DroitI, :Evacuation, :NumLicense)");
      	
       
       //REQUETES AFFICHAGE 
       //$this->ReqSelectCorps = $db->prepare("");
       
       $this->ReqSelectMenu = $db->prepare("SELECT menu FROM pagesite WHERE titre LIKE :name ");
    }


//Requêtes préparées
	public function InsertEcole($name, $city, $login, $password, $mail, $usep)
    {
        $this->ReqInsertEcole->execute(
            array(':name' => utf8_decode($name), ':city' => utf8_decode($city), ':login' => $login, ':password' => $password, ':mail' => $mail, ':usep'=>$usep));

        return $this->ReqInsertEcole->rowCount();
    }
	public function selectCorps(){
		$this->ReqSelectCorps->execute();
		return $this->ReqSelectCorps->fetchAll();
	}
    public function selectAllEcole()
    {
    	$this->ReqSelectAllEcole->execute();
        return $this->ReqSelectAllEcole->fetchAll();
    }

	public function selectById($id)
    {
        $this->ReqSelectById->execute(array($id));
        return $this->ReqSelectById ->fetch();
    }

    public function selectByName($name)
    {
        $this->ReqSelectByName->execute(array('%'.$name.'%'));
        return $this->ReqSelectByName->fetch();
    }
    public function Validation($name, $ville)
    {
    	$this->validationinsc->execute(array(':nom'=>utf8_decode($name), ':ville'=>utf8_decode($ville)));
    	return $this->validationinsc->rowCount();
    }
    
    public function SelectMenu($nompage)
    {
    	$this->ReqSelectMenu->execute(array(':name'=>$nompage));
    	return $this->ReqSelectMenu->fetch(PDO::FETCH_OBJ);
    }
    public function selectpage($nompage)
    {
    	$this->Reqpage->execute(array(':titre'=>$nompage));
    	return $this->Reqpage->fetch();
    }
    
    public function Connexion($login, $password){
    	$this->connexion->execute(array(':login'=>$login, ':password'=>$password));
    	return $this->connexion->fetch();
    }
 
    public function InsertEleve($idcourse, $idecole, $nomeleve, $annee, $adresse, $certi, $autoriP, $DroitImage, $Evacuation, $NumLicense)
    {
    	$this->ReqInsertEleve->execute(
    			array(':idcourse'=>$idcourse, ':idecole'=>$idecole, ':nomeleve'=>$nomeleve, ':annee'=>$annee, ':adresse'=>$adresse, ':certi'=>$certi, ':autoriP'=>$autoriP, ':DroitI'=>$DroitImage, ':Evacuation'=>$Evacuation, 'NumLicense'=>$NumLicense));
		  
    }
	public function truncate()
    {
        return $this->ReqTruncate->execute();
    }
    
    
	
}